package com.bshark.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.bshark.entry.Comment;
import com.bshark.entry.Content;
import com.bshark.util.DateUtil;
import com.bshark.util.JdbcPool;
import com.bshark.util.JsonParser;

public class ContextDao {
	
	Connection conn = null;
	PreparedStatement prep = null;
	ResultSet rs = null;
	
	/**
	 * 获取文章
	 * @param cid 客户端最后一条ID
	 * @param num 获取数目
	 * @param type 类型
	 * @return
	 */
	//data={"cmd":"1","cid":"1","num":"1","type":"1"}
	public List getContext(String data){
		String cid = JsonParser.get(data,"cid");
		if(cid==null||"".equals(cid)){
			cid = "0";
		}
		String type = JsonParser.get(data,"type");
		List list = new ArrayList();
		try {
			conn = JdbcPool.getInstance().getConnection();
			PreparedStatement state=conn.prepareStatement("select * from app_content where id>"+cid + " and rownum <=25 order by id");
			ResultSet rs=state.executeQuery();	
			while(rs.next()){
				Content content = new Content();
				content.id = rs.getInt("id");
				content.content=rs.getString("content");
				content.name=rs.getString("name");
				content.picUrl=rs.getString("picUrl");
				content.dateStr=rs.getString("dateStr");
				content.sex=rs.getByte("sex");
				content.wbType=rs.getByte("wbType");
				content.wbId=rs.getString("wbId");
				PreparedStatement state1=conn.prepareStatement("select count(0) from app_comment where cid="+content.id);
				ResultSet rs1=state1.executeQuery();
				if(rs1.next()){
					content.pnum = rs1.getInt(1);
				}
				list.add(content);
				rs1.close();
				state1.close();
			}
			rs.close();
			state.close();
			conn.close();
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		return list;
	}
	
	//public int id;
	//public String content;
	//public String name;
	//public String picUrl;
	//public String dateStr;
	//public Byte sex;
	//public int pnum;
	//public byte wbType;
	//public String wbId;
	//添加文章
	public String addContext(String data){
		int i = -1;
		String wbType = JsonParser.get(data,"wbType");
		String wbId = JsonParser.get(data,"wbId");
		String name = JsonParser.get(data,"name");
		String sex = JsonParser.get(data,"sex");
		String picUrl = JsonParser.get(data,"picUrl");
		String content = JsonParser.get(data,"content");
		
		if(wbType==null||"".equals(wbType)){
			return "wbType is null";
		}
		if(wbId==null||"".equals(wbId)){
			return "wbId is null";
		}
		if(name==null||"".equals(name)){
			return "name is null";
		}
		if(sex==null||"".equals(sex)){
			return "sex is null";
		}
		if(picUrl==null||"".equals(picUrl)){
			return "picUrl is null";
		}
		if(content==null||"".equals(content)){
			return "content is null";
		}
		
		try {
			conn = JdbcPool.getInstance().getConnection();
			prep = conn.prepareStatement("insert into app_content(id,content,name,picUrl,dateStr,wbType,wbId,sex) values(app_content_seq.Nextval,?,?,?,?,?,?,?)");
			prep.setString(1, content);
			prep.setString(2, name);
			prep.setString(3, picUrl);
			String dateStr = DateUtil.getDate();
			prep.setString(4, dateStr);
			prep.setString(5, wbType);
			prep.setString(6, wbId);
			prep.setString(7, sex);
			i = prep.executeUpdate();
			prep.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  String.valueOf(i);
	}
	
	
	//添加评论
	public String addComment(String data){
		//int type,String userid,String name,String content,int sex,int cid
		int i = -1;
		String wbType = JsonParser.get(data,"wbType");
		String wbId = JsonParser.get(data,"wbId");
		String name = JsonParser.get(data,"name");
		String sex = JsonParser.get(data,"sex");
		String picUrl = JsonParser.get(data,"picUrl");
		String content = JsonParser.get(data,"content");
		String cid = JsonParser.get(data,"cid");
		
		if(wbType==null||"".equals(wbType)){
			return "wbType is null";
		}
		if(wbId==null||"".equals(wbId)){
			return "wbId is null";
		}
		if(name==null||"".equals(name)){
			return "name is null";
		}
		if(sex==null||"".equals(sex)){
			return "sex is null";
		}
		if(picUrl==null||"".equals(picUrl)){
			return "picUrl is null";
		}
		if(content==null||"".equals(content)){
			return "content is null";
		}
		if(cid==null||"".equals(cid)){
			return "cid is null";
		}
		
		try {
			conn = JdbcPool.getInstance().getConnection();
			prep = conn.prepareStatement("insert into app_comment(id,cid,content,wbType,wbId,picUrl,name,sex) values(app_comment_seq.Nextval,?,?,?,?,?,?,?)");
			prep.setInt(1, Integer.valueOf(cid));
			prep.setString(2, content);
			prep.setString(3, wbType);
			prep.setString(4, wbId);
			prep.setString(5, picUrl);
			prep.setString(6, name);
			prep.setString(7, sex);
			i = prep.executeUpdate();
			prep.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return String.valueOf(i);
	}
	
	//获取评论
	public List getComment(String data){
		String cid = JsonParser.get(data,"cid");
		if(cid==null||"".equals(cid)){
			return null;
		}
		String pid = JsonParser.get(data,"pid");
		if(pid==null||"".equals(pid)){
			pid = "0";
		}
		List list = new ArrayList();
		try {
			conn = JdbcPool.getInstance().getConnection();
			PreparedStatement state=conn.prepareStatement("select * from app_comment where id >"+ pid + " and cid ="+cid + " and rownum<=15 order by id");
			ResultSet rs=state.executeQuery();	
			while(rs.next()){
				Comment comment = new Comment();
				comment.id = rs.getInt("id");
				comment.cid = rs.getInt("cid");
				comment.content=rs.getString("content");
				comment.name=rs.getString("name");
				comment.picUrl=rs.getString("picUrl");
				comment.sex=rs.getByte("sex");
				comment.wbType=rs.getByte("wbType");
				comment.wbId=rs.getString("wbId");
				list.add(comment);
			}
			rs.close();
			state.close();
			conn.close();
		} catch (Exception e) {
		}
		return list;
	}
}
